package com.itheima.controller;


import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.SaveDishDto;
import com.itheima.pojo.Dish;
import com.itheima.service.DishService;
import com.itheima.vo.DishDownVo;
import com.itheima.vo.DishPageVo;
import com.itheima.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author Administrator
 */
@RestController
@RequestMapping("dish")
public class DishController {

    @Autowired
    private DishService dishService;

    /**
     * 新增菜品
     * @param dto dto
     * @return R
     */
    @PostMapping("")
    public R<String> saveDish(@RequestBody SaveDishDto dto) {

        Boolean save = dishService.save(dto);

        return R.success("新增菜品成功");
    }

    /**
     * 分页查询
     * @param name
     * @param page
     * @param pageSize
     * @param type
     * @return
     */
    @GetMapping("page")
    public Result page(@RequestParam(required = false)String name,
                       @RequestParam(value = "page",defaultValue = "0") Integer page,
                       @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                       @RequestParam(required = false)String type){

        Page<DishPageVo> page1 = dishService.getPage(page, pageSize, name);
        return new Result(1,null,page1,null);

    }

    /**
     * 修改
     * @param dish dish
     * @return R
     */
    @PutMapping("")
    public R<String> modify(@RequestBody Dish dish) {

        Boolean res = dishService.updateDish(dish);

        return R.success("修改成功");
    }

    /**
     * 根据id查询
     * @param id  id
     * @return r
     */
    @GetMapping("{id}")
    public R<Dish> getById(@PathVariable String id) {

        Dish dish = dishService.getById(id);

        return R.success(dish);
    }

    /**
     * 删除
     * @param ids ids
     * @return R
     */
    @DeleteMapping("")
    private R<String> deleteById(String[] ids) {
        if (ArrayUtil.isNotEmpty(ids)) {
            Boolean res = dishService.deleteDish(ids);
        }

        return R.success("成功");
    }

    /**
     * 启用禁用
     * @param status 状态
     * @param ids id
     * @return R
     */
    @PostMapping("status/{status}")
    public R<String> updateStatus(@PathVariable Integer status,
                                  @RequestParam String ids){

        Boolean res = dishService.updateStatus(ids, status);

        return R.success("操作成功");

    }

    @GetMapping("list")
    public R<List<DishDownVo>> list(@RequestParam(value = "categoryId",required = false) String categoryId,
                                    @RequestParam(value = "name",required = false) String name){

        if(StrUtil.isNotEmpty(categoryId)){
            List<DishDownVo> dishDownList = dishService.dishDownList(categoryId,name);
            return R.success(dishDownList);
        }

        return R.success(null);
    }


}
